package com.zhiyou100.dao;

import com.zhiyou100.entityclass.Student;
import com.zhiyou100.tool.Tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @packageName: StudentDao
 * @className: com.zhiyou100.dao
 * @Description: TODO Student dao 判断是否存在 获取一个 修改一个 添加一个 删除一个 获取所有
 * @author: yang
 * @date: 7/20/20
 */
public class StudentDao {
    /**
     * @Description: TODO 是否存在
     * @name: isExist
     * @param: [sid]
     * @return: boolean
     * @date: 7/20/20 17:59
     * @author: yang
     */
    public boolean isExist(int sid) {
        // 记录是否存在
        boolean flag = false;
        // con,sta,res
        Connection connection = Tools.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 发送模版
            statement = connection.prepareStatement("select sid from student where sid=?");
            statement.setInt(1, sid);
            resultSet = statement.executeQuery();
            flag = resultSet.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Tools.close(resultSet, statement, connection);
        }
        return flag;
    }

    /**
     * @Description: TODO 根据 id 获取一个
     * @name: getOne
     * @param: [id]
     * @return: com.zhiyou100.entityclass.Student
     * @date: 7/20/20 18:47
     * @author: yang
     */
    public Student getOne(int id) {
        Student student = new Student();

        Connection connection = Tools.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 发送模版
            statement = connection.prepareStatement("select * from student where sid=?");
            statement.setInt(1, id);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int sid = resultSet.getInt(1);
                String sname = resultSet.getString(2);
                String sex = resultSet.getString(3);
                int sage = resultSet.getInt(4);
                int scid = resultSet.getInt(5);
                student = new Student((long) sid, sname, sex, (long) sage, (long) scid);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Tools.close(resultSet, statement, connection);
        }

        return student;
    }

    /**
     * @Description: TODO 根据 id 删除一个
     * @name: delOne
     * @param: [id]
     * @return: boolean
     * @date: 7/20/20 18:56
     * @author: yang
     */
    public boolean delOne(int id) {
        if (!isExist(id)) {
            return false;
        } else {
            Connection connection = Tools.getConnection();
            PreparedStatement statement = null;
            int i = 0;
            try {
                statement = connection.prepareStatement("delete from student where sid=?");
                statement.setInt(1, id);
                i = statement.executeUpdate();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                Tools.close(null, statement, connection);
            }
            return i != 0;
        }
    }

    /**
     * @Description: TODO 根据 id 添加一个
     * @name: addOne
     * @param: [student]
     * @return: boolean
     * @date: 7/20/20 19:07
     * @author: yang
     */
    public boolean addOne(Student student) {
        if (isExist(Math.toIntExact(student.getSid()))) {
            // 如果存在，不添加
            return false;
        } else {
            // 不存在，添加
            Connection connection = Tools.getConnection();
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            int i = 0;
            try {
                statement = connection.prepareStatement("insert into student (sid, sname, sex, sage, scid) values (?,?,?,?,?);");
                statement.setInt(1, Math.toIntExact(student.getSid()));
                statement.setString(2, student.getSname());
                statement.setString(3, student.getSex());
                statement.setInt(4, Math.toIntExact(student.getSage()));
                statement.setInt(5, Math.toIntExact(student.getScid()));
                i = statement.executeUpdate();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                Tools.close(null, statement, connection);
            }
            return i != 0;
        }
    }

    /**
     * @Description: TODO 更改一个
     * @name: changeOne
     * @param: [student]
     * @return: boolean
     * @date: 7/20/20 19:14
     * @author: yang
     */
    public boolean changeOne(Student student) {
        if (isExist(Math.toIntExact(student.getSid()))) {
            boolean a = delOne(Math.toIntExact(student.getSid()));
            boolean b = addOne(student);
            System.out.println(a + " " + b);
            return a && b;
        } else {
            return false;
        }
    }

    /**
     * @Description: TODO 获取所有
     * @name: getAllStudent
     * @param: []
     * @return: java.util.ArrayList<com.zhiyou100.entityclass.Student>
     * @date: 7/20/20 19:21
     * @author: yang
     */
    public ArrayList<Student> getAllStudent() {
        ArrayList<Student> students = new ArrayList<>();

        Connection connection = Tools.getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int i = 0;
        try {
            statement = connection.prepareStatement("select sid from student;");
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int anInt = resultSet.getInt(1);
                students.add(getOne(anInt));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            Tools.close(resultSet, statement, connection);
        }
        return students;
    }

    /**
     * @Description: TODO 参数 合理性判断
     * @name: reasonableJustification
     * @param: [sid, sname, sex, sage, scid]
     * @return: boolean
     * @date: 7/23/20 11:24
     * @author: yang
     */
    public boolean reasonableJustification(String sid, String sname, String sex, String sage, String scid) {
        Student student = new Student();
        boolean sidFlag = student.sidFlag(sid);
        boolean snameFlag = student.snameFlag(sname);
        boolean sexFlag = student.sexFlag(sex);
        boolean sageFlag = student.sageFlag(sage);
        boolean scidFlag = student.scidFlag(scid);
        return sidFlag && snameFlag && sexFlag && sageFlag && scidFlag;
    }

    public static void main(String[] args) {
        StudentDao studentDao = new StudentDao();
        ArrayList<Student> allStudent = studentDao.getAllStudent();
        for (Student student : allStudent) {
            System.out.println(student);
        }

    }
}
